import merge from 'lodash/merge.js'

/**
 * 对列进行排序和属性合并
 * @param data
 * @param isForm
 */
export function generateColumns(data,isForm = false){
  if (!Array.isArray(data)) {
    console.error('generateColumns expects an array as the first argument.');
    return [];
  }
  // 拓展了 propInTable, propInForm
  const result = data.map((item) => {
    let obj = {}
    if (isForm && item.propInTable) {
      obj = { ...item.propInTable }
    }
    if (isForm && item.propInForm) {
      obj = { ...item.propInForm }
    }
    return merge(item, obj)
  })
  // 排序
  if (isForm) {
    return result.sort((a, b) => (b.orderInForm || 0) - (a.orderInForm || 0))
  } else {
    return result
  }
}

/**
 * 字典数据做一层包装
 * @param data
 * @returns {{}}
 */
export function generateDictData (data){
  return Object.entries(data).reduce((obj,[key,val])=>{
    obj[key] =  val.value ?  val.value : val
    return obj;
  },{})
}
